<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Company\Archive;
use App\Models\Company\BackstageCategory;
use App\Models\Encyclopedia\Article;
use App\Models\Encyclopedia\Category;
use App\Models\Link;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;

class IndexController extends Controller
{
    const BAIKE_CATEGORY = [
        '设计' => 'sheji',
        '施工' => 'shigong',
        '预算' => 'yusuan',
        '居家' => 'jujia'
    ];

    protected $baikeCategory;

    public function index()
    {
        if (is_domain_top()) {
            return $this->topDomain();
        }
        return $this->second();
    }

    private function topDomain()
    {
        $category = self::BAIKE_CATEGORY;
        $archive = Archive::with('region')->limit(20)->orderBy('updated_at', 'desc')->get();

        $url = request()->url();
        if (request()->get('clear', '')){
            \Cache::clear();
        }
        if (\Cache::offsetExists($url)) {
            $data = \Cache::get($url);
        } else {
            $this->baikeCategory = new Collection();
            foreach (self::BAIKE_CATEGORY as $key => $item) {
                $this->baikeCategory->put($key, $this->categoryArticle($item, 10));
            }
            $baikeCategory = $this->baikeCategory;

            $categorys = BackstageCategory::get();
            $categorys->each(function (BackstageCategory $backstageCategory){
                $backstageCategory->load('material.region');
            });
            $data = \Cache::remember($url, 60, function () use ($categorys, $baikeCategory) {
                return ['categorys' => $categorys, 'baikeCategory' => $baikeCategory];
            });
        }
        $categorys = $data['categorys'];
        $baikeCategory = $data['baikeCategory'];
        $link = Link::whereNull('region_id')->get();
        return view('index', compact(
            'archive', 'link', 'baikeCategory', 'category', 'categorys'
        ));
    }

    private function second()
    {
        $article = $this->inRandomOrder(6);
        $strategy = $this->inRandomOrder(5);
        $case = $this->inRandomOrder(12);
        $testing = $this->inRandomOrder(10);
        $city = config('region.region.model', '');
        $link = Link::where('region_id', $city->id ?? 38)->get();
        return view('region.index', compact('article', 'strategy', 'case', 'testing', 'link'));
    }

    protected function inRandomOrder($num)
    {
        $article = Article::inRandomOrder()->take($num)->get();
        $article = $article->map(function (Article $item) {
            $item->media->path = Storage::url($item->media->path);
            return $item;
        });
        return $article;
    }

    protected function categoryArticle($category, $num = 5)
    {
        $category = Category::where('code', $category)->first();
        $article = Article::where('category_id', $category->id ?? '')->take($num)->get();
        $article = $article->map(function (Article $item) {
            $item->media->path = Storage::url($item->media->path);
            return $item;
        });
        return $article;
    }
}
